package org.flower.web.servlet;

import com.github.pagehelper.PageInfo;
import org.flower.bean.CommodityDetails;
import org.flower.dao.CommodityClassDao;
import org.flower.dao.impl.CommodityClassDaoImpl;
import org.flower.entity.CommodityClass;
import org.flower.service.CommodityDetailsService;
import org.flower.service.impl.CommodityDetailsServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 商品选择servlet
 *
 * @author Aizen
 * @date 2022/01/02
 */
@WebServlet("/goodsSelectServlet.shtml")
public class GoodsSelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CommodityDetails commodityDetails = new CommodityDetails();
        CommodityClassDao ccdao = new CommodityClassDaoImpl();
        //获取所有商品的类别
        List<CommodityClass> comclassList = ccdao.getAllCommodityClass();
        //商品名称
        String goodsName = request.getParameter("goodsName");
        //获取商品类别
        String classid = request.getParameter("classid");
        if(classid==null){
            classid = "0";
        }
        commodityDetails.setClassid(Integer.parseInt(classid));

        //分页
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null){
            pageNo="1";
        }

        //sortNo_排序编号
        String sortNo = request.getParameter("sortNo");
        if(sortNo==null||sortNo.equals("")){
            sortNo = "0";
        }
        commodityDetails.setSortNo(Integer.parseInt(sortNo));


        commodityDetails.setComname(goodsName);
        //是查询页面来的
        commodityDetails.setFindFlag(true);

        CommodityDetailsService cds = new CommodityDetailsServiceImpl();
        PageInfo<CommodityDetails> goods = cds.findAll(commodityDetails, Integer.parseInt(pageNo));
        List<CommodityDetails> list = goods.getList();
        list.forEach(commodityDetails1 -> {
            String content = commodityDetails1.getContent();
            String[] split = content.split("split");
            commodityDetails1.setContent(split[0]);
            commodityDetails1.setContent2(split[1]);
            String comimage = commodityDetails1.getComimage();
            String[] split1 = comimage.split(",");
            commodityDetails1.setComimage(split1[0]);
        });
        goods.setList(list);
        request.setAttribute("classid",classid);
        request.setAttribute("sortNo",sortNo);
        request.setAttribute("goodsName",goodsName);
        request.setAttribute("goods",goods);
        request.setAttribute("comclassList",comclassList);
        request.getRequestDispatcher("goodsSelect.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }


}
